package com.blb.community.controller;

import com.blb.community.Annotation.Log;
import com.blb.community.entity.ZyRepair;
import com.blb.community.service.IZyRepairService;
import com.blb.community.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报修信息 前端控制器
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@CrossOrigin
@RestController
@RequestMapping("/repair")
public class ZyRepairController {

    @Autowired
    private IZyRepairService zyRepairService;

    /**
     * 查询报修列表和分页
     * @param zyRepair
     * @return
     */
    @PostMapping("/selectRepair")
    public ResponseResult<Map<Object, Object>> selectRepair(@RequestBody ZyRepair zyRepair) {
        System.out.println("zyRepair = " + zyRepair);

        HashMap<Object, Object> map = new HashMap<>();
        List<ZyRepair> zyRepairList = zyRepairService.selectZyRepairList(zyRepair, 0, 0);

        //总条数
        int total = zyRepairList.size();
        int pages = total % zyRepair.getPageSize() == 0 ? total / zyRepair.getPageSize() : total / zyRepair.getPageSize() + 1;
        if (zyRepair.getNowPage() > pages) {
            zyRepair.setNowPage(pages);
        }
        //页码修正
        if (zyRepair.getNowPage() < 1) {
            zyRepair.setNowPage(1);
        }
        //重新赋值
        zyRepairList = zyRepairService.selectZyRepairList(zyRepair, zyRepair.getNowPage(), zyRepair.getPageSize());

        map.put("zyRepairList", zyRepairList);
        map.put("total", total);
        map.put("pages", pages);
        map.put("nowPage", zyRepair.getNowPage());
        map.put("pageSize", zyRepair.getPageSize());

        return ResponseResult.ok(map);
    }
    @Log(module = "修改", operationType = 2, requestMethod = "PUT")
    @PutMapping("/updateRepair")
    public ResponseResult<String> updateRepair(@RequestBody ZyRepair zyRepair){
        zyRepairService.updateRepair(zyRepair);
        return ResponseResult.ok("修改成功");
    }
}
